package stack;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class DailyTemperatures {
    public static void main(String[] args){
        int[] temperatures = {73,74,75,71,69,72,76,73};
        int length = temperatures.length;
        int[] ans = new int[length];
//        可以维护一个存储下标的单调栈，从栈底到栈顶的下标对应的温度列表中的温度依次递减
        Deque<Integer> stack = new LinkedList<Integer>();
        for (int i = 0; i < length; i++) {
            int temperature = temperatures[i];
            while (!stack.isEmpty() && temperature > temperatures[stack.peek()]) {
                int prevIndex = stack.pop();
                ans[prevIndex] = i - prevIndex;
            }
            stack.push(i);
        }

        System.out.println(Arrays.toString(ans));
    }
}
